package com.xinghou.platform.dao.mange.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.mange.PhotoDAO;
import com.xinghou.platform.model.main.Photo;
/**
 * @author liuwei.wen
 *
 */
@Repository
public class PhotoDAOImpl implements PhotoDAO {
	@Resource
	private JdbcTemplate jdbcTemplate;
	

	@Override
	public Photo photo(Integer id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
	       String sql="select Id,User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time from photo where Id=?";
	        Photo Photo=(Photo) jdbcTemplate.queryForObject(sql, new Object[] {id}, new PhotoMapper());
			return Photo;
	}
	private static final class PhotoMapper implements RowMapper<Photo> {
		public Photo mapRow(ResultSet rs, int rowNum) throws SQLException {
			Photo photo = new Photo();
			photo.setAlbumId(rs.getInt("Album_id"));
			//photo.setCreateTime(rs.getTimestamp("Create_time"));
			photo.setId(rs.getInt("Id"));
			photo.setName(rs.getString("NAME"));
			photo.setPhotoUri(rs.getString("Photo_uri"));
			photo.setPraiseCount(rs.getInt("Praise_count"));
			photo.setStatus(rs.getShort("STATUS"));
			//photo.setUpdateTime(rs.getTimestamp("Update_time"));
			photo.setUserId(rs.getInt("User_id"));
			return photo;
		}
	}

}
